package org.sgx.gwteditors.client.impl1.misc;

import java.util.List;

import org.sgx.gwteditors.client.editor.ConfigurableEditor;
import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.util.JsObject;
//import org.sgx.jsutil.client.JsObject;

import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/**
 * an editor for image urls - it will show the image. 
 * @author sg
 *
 */
public class ImageUrlEditor extends VerticalPanel implements Editor<String>, 
ValueChangeHandler<String>, ConfigurableEditor {
	
	private TextBox textBox;
	private Image image;
	boolean editable=false;

	@Override
	public boolean isSimple() {
		return true;
	}
	public ImageUrlEditor() {
		super();
		textBox = new TextBox();
		add(textBox); 
		image = new Image();
		add(image); 
//		anchor.setText("link->"); 
		textBox.addValueChangeHandler(this);
	}
	@Override
	public String flush() {
		return textBox.getText();
		//no validation / errors
	}

	@Override
	public void load(String t) {
		if(!isEditable())
			textBox.setVisible(false); 
		else
			textBox.setVisible(true);
		textBox.setText(t);
		image.setUrl(t); //.setHref(t); 
	}

	@Override
	public Widget getWidget() {
		return this;
	}

	@Override
	public void addValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String>getInstance().addValueChangeListener(this, l);
		image.setUrl(textBox.getText());
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String>getInstance().removeValueChangeListener(this, l);
	}
	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		EditorEventManager.<String>getInstance().fireValueChangeGwtEvent(this, event);	
	}
	@Override
	public List<EditorError<String>> getErrors() {
		return EditorErrorManager.<String>getInstance().getErrors(this);
	}
	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_STRING}; 
	}
	public boolean isEditable() {
		return editable;
	}
	public void setEditable(boolean editable) {
		this.editable = editable;
	}
	@Override
	public void config(String configStr) {
		JsObject jso = JsonUtils.unsafeEval("{"+configStr+"}"); 
		if(jso.objGetBoolean("editable"))
			editable=true; 
		else
			editable=false;
		if(jso.objGetString("width")!=null)
			image.setWidth(jso.objGetString("width")); 
		if(jso.objGetString("height")!=null)
			image.setHeight(jso.objGetString("height")); 
	}
	
	
}
